#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <atomic>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <boost/algorithm/string.hpp>
#include "Logger.hpp"


namespace ns_util
{
    using namespace ns_log;
    static const std::string temp = "./temp/";
    static const std::string questions_path = "./questions/";
    class PathUtil
    {
    private:
        // ./temp/filename.suffix
        static std::string AddSuffix(const std::string &filename, const std::string &suffix)
        {
            if (suffix.empty())
                return std::string();
            std::string path = temp;
            path += filename;
            path += suffix;
            return path;
        }

        static std::string QuestionAddSuffix(const std::string &dirname, const std::string &filename, const std::string &suffix)
        {
            if (suffix.empty() || dirname.empty())
                return std::string();
            // ./questions/1/dest.txt
            std::string path = questions_path;
            path += dirname;
            path += "/";
            path += filename;
            path += suffix;
            return path;
        }

    public:
        // 编译模块的文件名suffix
        // file.cpp
        static std::string Src(const std::string &filename)
        {
            return AddSuffix(filename, ".cpp");
        }
        // file.exe
        static std::string Exe(const std::string &filename)
        {
            return AddSuffix(filename, ".exe");
        }
        // file.stderr
        static std::string compile_error(const std::string &filename)
        {
            return AddSuffix(filename, ".compile_error");
        }

        // 运行模块所使用的文件名suffix
        static std::string Stdin(const std::string &filename)
        {
            return AddSuffix(filename, ".stdin");
        }
        static std::string Stdout(const std::string &filename)
        {
            return AddSuffix(filename, ".stdout");
        }
        static std::string Stderr(const std::string &filename)
        {
            return AddSuffix(filename, ".stderr");
        }

        // oj所使用的文件
        static std::string DestTxt(const std::string &dirname, const std::string &filename)
        {
            return QuestionAddSuffix(dirname, filename, ".txt");
        }

        static std::string HeaderSRC(const std::string &dirname, const std::string &filename)
        {
            return QuestionAddSuffix(dirname, filename, ".cpp");
        }

        static std::string TailSRC(const std::string &dirname, const std::string &filename)
        {
            return QuestionAddSuffix(dirname, filename, ".cpp");
        }
    };

    class TimeUtil
    {
    public:
        static std::string GetTimeTamp()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec);
        }
        // 获取毫秒时间戳
        static std::string GetTimeMs()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec * 1000 + tv.tv_usec / 1000);
        }
    };

    class FileUtil
    {
    public:
        static bool FileIsExit(const std::string &filename)
        {
            struct stat src_stat;
            return stat(filename.c_str(), &src_stat) == 0;
        }

        static std::string GetUniqueFileName()
        {
            static std::atomic_uint id(0);
            id++;
            // 毫秒级时间戳+原子性递增唯一值: 来保证唯一性
            std::string time = TimeUtil::GetTimeMs();
            return std::to_string(id) + "_" + time;
        }

        static bool Write2File(const std::string &filename, const std::string &code, const std::string &input)
        {
            std::ofstream out(filename);
            if (!out.is_open())
            {
                LOG(LogLevel::WARNING) << filename << " open err";
                return false;
            }

            out.write(code.c_str(), code.size());
            out.close();
            return true;
        }

        static bool ReadFile(const std::string &filename, std::string *content, bool keep)
        {
            if (!FileIsExit(filename))
                return false;
            content->clear();
            std::ifstream in(filename);
            if (!in.is_open())
            {
                return false;
            }

            std::string line;
            // getline，读取默认是没有"\n"
            // getline, 重载了类型推导
            while (std::getline(in, line))
            {
                *content += (line);
                // 是否要加"\n"
                *content += (keep ? "\n" : "");
            }
            in.close();
            return true;
        }

        static void ResourceClean(const std::string &filename)
        {
            // 删除所有以前缀 filename 开头的文件 并且忽视标准错误(黑洞)
            std::string cmd = "rm -f ./temp/" + filename + "* 2>/dev/null";
            int result = system(cmd.c_str());
            (void) result;
        }
    };

    class BoostUtil
    {
    public:
        static void CuttingSubstrings(std::vector<std::string> *out, const std::string &origin_string, const std::string &sep)
        {
            boost::split(*out, origin_string, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}